package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.UiUtils;

/**
 * 生成协议书温馨提示，提示挪车，Created by Administrator on 2016/5/12.
 */

public class PromptAfterCreateAppointView extends LFView implements View.OnClickListener {
    private Context context = null;
    private View view = null;
    private Button bt_promptaftercreateappoint_ensure;
    private String appCaseno;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private BaseApplication mApplication;
    private EntityBean myDrivingLiscenceBean;//驾驶证的bean
    private String isSingleCar;

    public PromptAfterCreateAppointView(Context c, String appcaseno) {
        super(c);
        this.context = c;
        this.appCaseno = appcaseno;
        this.mApplication = ((MainActivity) c).getAppData();
    }

    public void setIsSingleCar(String isSingleCar) {
        this.isSingleCar = isSingleCar;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_promptaftercreateappoint, null);
        bt_promptaftercreateappoint_ensure = (Button) view.findViewById(R.id.bt_promptaftercreateappoint_ensure);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("温馨提示");

        ((MainActivity) context).initHelpView(view, appCaseno);
    }

    private void regEvent(boolean b) {
        if (null != bt_promptaftercreateappoint_ensure) {
            bt_promptaftercreateappoint_ensure.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_promptaftercreateappoint_ensure:
                checkDrivingLicence();
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;

        }
    }

    //根据车主姓名查询驾驶证信息
    private void checkDrivingLicence() {
        final Dialog processDialog2 = UiUtils.showProcessDialogPro(getContext(), "加载数据中...");
        EntityBean bean = new EntityBean();
        EntityBean userInfo = mApplication.getLoginInfo().getBean("userinfo");
        bean.set("uid", mApplication.getKckpName());
        bean.set("carownername", userInfo.get("name"));
        bean.set("token", mApplication.getToken());
        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.KCKPAPPGETCAROWNER, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog2) {
                            if (processDialog2.isShowing()) {
                                processDialog2.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            myDrivingLiscenceBean = bean.getBean("data");
                            if (TextUtils.equals("0", isSingleCar)) {//单车事故
                                SingleAccidentLoadingView singleaccidentloadingview = new SingleAccidentLoadingView(context, appCaseno, myDrivingLiscenceBean);
                                FrameworkManager.getInstance().showNewForm(context, singleaccidentloadingview);
                            } else {//双车事故
                                //进入事故类型选择页面
                                NewSelectAccidentView selectaccidentview = new NewSelectAccidentView(context, appCaseno, myDrivingLiscenceBean);
                                FrameworkManager.getInstance().showNewForm(context, selectaccidentview);
                            }
                        } else {
                            UiUtil.showToast(context, redes);
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog2) {
                            if (processDialog2.isShowing()) {
                                processDialog2.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog2) {
                            if (processDialog2.isShowing()) {
                                processDialog2.dismiss();
                            }
                        }
                    }
                });
    }

}
